//
//  StatusOriginalView.swift
//  SinaWeiBo
//
//  Created by dengchen on 15/11/14.
//  Copyright © 2015年 name. All rights reserved.
//

import UIKit
import SnapKit


class StatusOriginalView: UIView {

    var bottomConstraints:Constraint?
    
    var status:statusModel? {
        didSet {
            
            updateUI()
            setupBottomView()

        }
    }
    
    
    private func updateUI() {
        
        iconView.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
        if status?.user?.mbrank > 0 {
            nameView.textColor = defaultColor
        }else {
            nameView.textColor = UIColor.blackColor()
        }
        nameView.text = status?.user?.screen_name
        timeView.text = status?.created_at
        comeFromView.text = status?.source
        crownView.image = status?.user?.mbrankImage
        ctView.text = status?.text
        verifiedIconView.image = status?.user?.verified_type_Image
        
        
    }
    
    
    private func setupBottomView() {
        
        self.bottomConstraints?.uninstall()
        
        //判断是否这条微博数据是否含有图片,有和没有都要做出对应的处理防止重用
        if let picurls = status?.imageURLs where status?.imageURLs?.count > 0 {
            
            pictureView.hidden = false
            pictureView.imageURLs = picurls
            
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.bottomConstraints = make.bottom.equalTo(pictureView).offset(CellMargins).constraint
            })
            
        }else {
            
            pictureView.hidden = true
            
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.bottomConstraints = make.bottom.equalTo(ctView).offset(CellMargins).constraint
            })
        }
    }
    
    
    private func setupUI() {
        addSubview(iconView)
        addSubview(nameView)
        addSubview(timeView)
        addSubview(comeFromView)
        addSubview(crownView)
        addSubview(ctView)
        addSubview(verifiedIconView)
        addSubview(pictureView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(CellMargins)
            make.width.equalTo(CellIconSize)
            make.height.equalTo(CellIconSize)
        }
        
        verifiedIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        nameView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(CellMargins)
        }
        
        timeView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(CellMargins)
        }
        
        comeFromView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(timeView.snp_right).offset(CellMargins)
        }
        
        crownView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(nameView.snp_right).offset(CellMargins)
        }
        
        ctView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(CellMargins)
            make.right.equalTo(self).offset(-CellMargins)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(ctView)
            make.top.equalTo(ctView.snp_bottom).offset(CellMargins)
        }
        
        self.snp_makeConstraints { (make) -> Void in
         self.bottomConstraints = make.bottom.equalTo(pictureView).offset(CellMargins).constraint
        }
    }
    

    override init(frame: CGRect) {
        super.init(frame: CGRectZero)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:加载控件

    //头像
    private let iconView = UIImageView()
    
    //用户名字
    private let nameView = UILabel(title:"11", Color: UIColor.blackColor(), fontSize: 15,textAlignment: .Left)
    
    //认证类型用户
    private let verifiedIconView = UIImageView()
    
    //发布时间
    private let timeView = UILabel(title: "11:11", Color: UIColor.orangeColor(), textAlignment:.Left)
    
    //来自啥啥啥
    private let comeFromView = UILabel(title: "隔壁老王", Color: UIColor.lightGrayColor(), fontSize: 12, textAlignment: .Left)
    
    //微博内容
    private let ctView = UILabel(title: "转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博", Color: UIColor.blackColor(), fontSize: 15, textAlignment: .Left)
    
    //皇冠
    private let crownView = UIImageView()
    
    //图片
    private let pictureView = StatusPictureView()
    

}
